/**
 * <b>项目名：</b>hclv-web<br/>  
 * <b>包名：</b>com.flea.hclv.service.impl<br/>  
 * <b>文件名：</b>HServicePayItemServiceImpl.java<br/>  
 * <b>版本信息：</b>1.0<br/>  
 * <b>日期：</b>2018年11月16日 下午7:40:11<br/>  
 * <b>COPYRIGHT 2010-2012 ALL RIGHTS RESERVED 虫子科技</b>-版权所有<br/>
 */
package com.flea.hclv.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.dao.HServiceOrderMapper;
import com.flea.hclv.model.dao.HServicePayItemMapper;
import com.flea.hclv.model.entity.HServiceOrder;
import com.flea.hclv.model.entity.HServicePayItem;
import com.flea.hclv.service.HServicePayItemService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.base.util.StringUtil;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.FleaStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @description 订单业务支付功能处理
 * @createTime 2018年11月16日 下午7:40:11
 * @modifyTime 
 * @author myflea@163.com
 * @version 1.0
 */
@Service
public class HServicePayItemServiceImpl implements HServicePayItemService {
	@Autowired
	private HServicePayItemMapper hServicePayItemMapper;
	@Autowired
	private HServiceOrderMapper hServiceOrderMapper;

	@Override
	public ServerResponse<List<HServicePayItem>> addPayment(HttpServletRequest request, String model) throws Exception {
		if(StringUtil.isEmpty(model)){
			return ServerResponse.createByErrorMessage("传入的数据非法，请检查填写的支付信息");
		}
		JSONObject obj = JSONObject.parseObject(model);
		String orderNo = obj.getString("orderNo");
		if(null==orderNo){
			return ServerResponse.createByErrorMessage("订单号为空，请重新输入");
		}
		
		HServiceOrder waitPayOrder =  hServiceOrderMapper.findByOrderNo(orderNo);
		if(null==waitPayOrder){
			return ServerResponse.createByErrorMessage("该订单不存在");
		}
		
		
		String objPayItems = String.valueOf(obj.get("payItems"));
		List<HServicePayItem> payItems = JSONObject.parseArray(objPayItems, HServicePayItem.class);
		if(null==payItems || payItems.size()==0){
			return ServerResponse.createByErrorMessage("付款信息为空");
		}
		
		String userId = FleaOrgUtils.getCurUserUuid(request);
		String orgId = FleaOrgUtils.getCurrentUserDeptUuid(request);
		String receiveId = waitPayOrder.getSupplerOrgId();
		Float payedMoney = waitPayOrder.getActualPayment();
		if(null==payedMoney){
		  payedMoney = 0f;
    }
		Float thisTotelPay = 0f;
		List<HServicePayItem> newInsertBatch = new ArrayList<HServicePayItem>();
		for (HServicePayItem hServicePayItem : payItems) {
			thisTotelPay=+hServicePayItem.getPayMoney();
			hServicePayItem.setCreateUser(userId);
			hServicePayItem.setId(FleaStringUtils.getGenerateUUID());
			hServicePayItem.setModifyUser(userId);
			hServicePayItem.setOrderNo(orderNo);
			hServicePayItem.setPayOrgId(orgId);
			hServicePayItem.setReceiveId(receiveId);
			newInsertBatch.add(hServicePayItem);
		}
		//插入数据库
		Float actualMoney = thisTotelPay+ payedMoney;
		waitPayOrder.setActualPayment(actualMoney);
		Float resShouldPay = waitPayOrder.getShouldPayment()-waitPayOrder.getActualPayment();
		//支付完成
		if(resShouldPay<=0){
			waitPayOrder.setPayStatus(HConstant.D_HCLV_D_SERVICE_ORDER_STATIC_PAT_ALL);
		}
		else{
			waitPayOrder.setPayStatus(HConstant.D_HCLV_D_SERVICE_ORDER_STATIC_PARTPRY);
		}
		//g更新数据库
		boolean result = hServiceOrderMapper.updateByPrimaryKeySelective(waitPayOrder)==1;
		if(!result){
			throw new Exception("支付更新订单失败！");
		}
		
		int insertResult = hServicePayItemMapper.insertBatch(newInsertBatch);
		if(newInsertBatch.size()!=insertResult){
			throw new Exception("更新支付记录信息失败");
		}
		
		return ServerResponse.createBySuccess("支付成功", newInsertBatch);
	}

  @Override
  public ServerResponse<List<HServicePayItem>> findByOrderNo(String orderNo) throws Exception {
	  List<HServicePayItem> result = coverCodeToName(hServicePayItemMapper.findByOrderNo(orderNo));
    return ServerResponse.createBySuccess(result);

  }

  private List<HServicePayItem> coverCodeToName(List<HServicePayItem> oldList){
	  List<HServicePayItem> newItems = new ArrayList<>();
    for (HServicePayItem item: oldList  ) {
      String ss = FleaOrgUtils.getOrgDeptByDeptUuid(item.getPayOrgId()).getDeptName();
      item.setPayOrgName(ss);
      item.setReceiveOrgName(FleaOrgUtils.getOrgDeptByDeptUuid(item.getReceiveId()).getDeptName());
      newItems.add(item);
    }
	  return newItems;
  }


}
